// obj记录+forEach遍历
function counts(arr) {
  const obj = {};
  arr.forEach(item => {
    // obj[item] = obj[item] ? obj[item] + 1 : 1
    obj[item] = (obj[item] || 0) + 1
  })
  return obj;
}

// reduce遍历
function counts(arr) {
  return arr.reduce((pre,curr)=>{
    // pre[curr] = pre[curr] ? pre[curr] + 1 : 1
    pre[curr] = (pre[curr] || 0) + 1
    return pre;
  }, {})
}
const numberArray = [1, 1, 1, 2, 2, 3, 3, 4, 5];
const stringArray = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'];
console.log(counts(numberArray))
console.log(counts(stringArray))